package com.geekbang.work9;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class DataSourceAspect {

    @Before("@annotation(dataSource)")
    public void changeDataSource(JoinPoint point, DataSource dataSource) throws Throwable {
        String value = dataSource.value();
        if (value.equals("master")){
            DataSourceType.setDataBaseType(DataSourceType.DataBaseType.MASTER);
        }else if (value.equals("slave")){
            DataSourceType.setDataBaseType(DataSourceType.DataBaseType.SLAVE);
        }else {
            DataSourceType.setDataBaseType(DataSourceType.DataBaseType.MASTER);//默认使用主数据库
        }

    }

    @After("@annotation(dataSource)") //清除数据源的配置
    public void restoreDataSource(JoinPoint point, DataSource dataSource) {
        DataSourceType.clearDataBaseType();
    }
}
